import {get} from '~/plugins/axios';

const dictionary = {
  custom: {
    scale: 'company_scale',
    trades: 'trade',
    type: 'company_type',
  },
  user: { lx: 'sys_user_lx', xb: 'sys_user_xb' },
  cangku: { user_xb: 'sys_user_xb' },
  assistGoods: { xfzt: 'wms_assist_delivery_xfzt' },
  huozhu: { bhdsbh: 'wms_owner_bhdsbh',
    bhzdqr: 'wms_owner_bhzdqr',
    dcfh: 'wms_owner_dcfh',
    djpda: 'wms_owner_djpda',
    kcyj: 'wms_owner_kcyj',
    pdwwcqx: 'wms_owner_pdwwcqx',
    shfk: 'wms_owner_shfk',
    shscpch: 'wms_owner_shscpch',
    shscscrq: 'wms_owner_shscscrq',
    shzdmp: 'wms_owner_shzdmp',
    zxgx: 'wms_owner_zxqx',
    sfbh: 'wms_owner_sfbh',
    zjfs: 'wms_owner_zjfs',
    yxj: 'wms_owner_yxj',
    slbuck: 'wms_owner_slbuck',
    djdps: 'wms_owner_djdps',
    ckfh: 'wms_owner_ckfh',
    cz: 'wms_owner_cz',
    fhdymd: 'wms_owner_fhdymd',
    zcd: 'wms_owner_zcd',
    cmhpm: 'wms_owner_cmhpm',
    smLpn: 'wms_owner_smLpn',
    zyzdqr: 'wms_owner_zyzdqr' },
  cxkd: {jhfs: 'wms_shipment_order_jhfs',
    cyslb: 'wms_shipment_order_cyslb',
  },
  huopinzhonglei: { sfhf: 'wms_goods_type_sfyx' },
  menu: { sq: 'sys_menu_sq' },
  orderChannel: { sq: 'sys_menu_sq' },
  datas: {
    dType: 'datas_type', // 菜单类型
  },
  kuwei: { hphf: 'wms_librarypositionhphf',
    lx: 'wms_librarypositionlx',
    pchf: 'wms_librarypositionpchf',
    zyzt: 'wms_libraryposition_zyzt',
    sx: 'wms_librarypositionsx' },
  goods: {
    bkxs: 'wms_goods_bkxs',
    cjsn: 'wms_goods_cjsn',
    sfyx: 'wms_goods_sfyx',
    zhhp: 'wms_goods_zhhp',
    yxqkz: 'wms_goods_yxqkz',
    zqlx: 'wms_goods_zqlx',
    jhwzlx: 'wms_goods_jhwzlx',
    jhwnlx: 'wms_goods_jhwzlx',
    jhxnlx: 'wms_goods_jhxnlx',
    jhtnlx: 'wms_goods_jhtnlx',
    bhdw: 'wms_goods_bhdw',
    ccqbh: 'wms_goods_ccqbh',
    xjhwbh: 'wms_goods_xjhwbh',
    qzfs: 'wms_goods_qzfs',
    bom: 'wms_goods_bom',
  },
  log: {jb: 'sys_log_jb', clzt: 'sys_log_clzt'},
  shoufaleibie: { sfzj: 'wms_owner_ordertype_sfzj',
    fl: 'wms_owner_ordertype_fl' },
  turnoverbox: { sfjh: 'wms_turnover_box_sfjh' },
  purchase: {zt: 'wms_purchase_order_zt'},
  daohuo: {asnzt: 'wms_estimated_arrival_order_asnzt',
    dyzt: 'wms_estimated_arrival_order_dyzt',
    mpdyzt: 'wms_estimated_arrival_order_mpzt',
    yxj: 'wms_estimated_arrival_order_yxj',
    shzt: 'wms_estimated_arrival_order_shzt'},
  customerFile: {
    khlx: 'wms_customer_file_khlx',
    sfjh: 'wms_customer_file_sfjh',
  },
  basegroup: {sfyx: 'wms_librarypositionlx', kwzlx: 'wms_librarypositiongroup_kwzlx'},
  shipment: {
    bczt: 'wms_shipment_order_bczt',
    cdzt: 'wms_shipment_order_cdzt',
    fyzt: 'wms_shipment_order_zt',
    jhzt: 'wms_shipment_order_jhzt',
    sfyx: 'wms_shipment_order_sfyx',
    shzt: 'wms_shipment_order_shzt',
    yxj: 'wms_shipment_order_yxj',
    zxzt: 'wms_shipment_order_zxzt',
    fymxzt: 'wms_shipment_order_detailed_zt',
    jhfs: 'wms_quality_testing_jhfs',
    cyslb: 'wms_shipment_order_cyslb',
  },
  zhijian: {
    fs: 'wms_quality_testing_fs',
  },
  orderReplenishment: {
    zt: 'wms_order_repair_zt',
  },
  shelvesTask: {
    account: 'account',
  },
  qualityOutOperationTask: {
    ckrwzt: 'wms_quality_inspection_list_ckrwzt',
  },
  qualityInOperationTask: {
    rkrwzt: 'wms_quality_inspection_list_rkrwzt',
    zt: 'wms_quality_inspection_list_zt',
  },
  qualityList: {
    zt: 'wms_quality_inspection_list_zt',
    ckrwzt: 'wms_quality_inspection_list_ckrwzt',
    rkrwzt: 'wms_quality_inspection_list_rkrwzt',
  },
  artificialReplenishment: {
    zt: 'wms_artificial_replenishment_zt',
  },
  stockTransfer: {
    ydzt: 'wms_stock_move_ydzt',
  },
  stockFreezing: {
    zt: 'wms_stock_frozen_zt',
  },
  inventoryPlan: {
    lx: 'wms_inventory_lx',
    qcpd: 'wms_inventory_qcpd',
    dtpd: 'wms_inventory_dtpd',
    kkw: 'wms_inventory_kkw',
    bhgdkw: 'wms_inventory_bhgdkw',
    mp: 'wms_inventory_mp',
    dppdzt: 'wms_inventory_dppdzt',
    sjpd: 'wms_inventory_sjpd',
    rwzt: 'wms_inventory_rwzt',
    tzzt: 'wms_inventory_tzzt',
  },
  pickTask: {
    jhrwzt: 'wms_shipment_order_jhrwzt',
  },
  fastOutput: {
    fyzt: 'wms_quick_exit_fyzt',
  },
};
function autoDictionAryState (dictionary) {
  let step = {};
  for (let index in dictionary) {
    if (dictionary.hasOwnProperty(index)) {
      step[index] = false;
    }
  }
  return step;
}

function autoDictionAryMutations (state) {
  let step = {};
  for (let index in state) {
    if (state.hasOwnProperty(index)) {
      step['SET_' + index] = function (state) {
        state[index] = true;
      };
      step['DEL_' + index] = function (state) {
        state[index] = false;
      };
    }
  }
  return step;
}

function autoDictionAryActions (state, dictionary) {
  let step = {};
  for (let index in state) {
    if (state.hasOwnProperty(index)) {
      step['USE_' + index] = async function ({ state, commit }) {
        if (!state[index]) {
          try {
            await (() => {
              let i = 0;
              for (let key in dictionary[index]) {
                if (!dictionary[index].hasOwnProperty(key)) return;
                i++;
              }
              return new Promise(function (resolve, reject) {
                for (let key in dictionary[index]) {
                  if (dictionary[index].hasOwnProperty(key)) {
                    (async () => {
                      commit(index.toUpperCase() + '/SET_' + key, await get('/sysdictionary/getSysdictionary/' + (dictionary[index][key] || key)), { root: true });
                      if (--i === 0) {
                        resolve();
                      }
                    })();
                  }
                }
              });
            })();
            commit('SET_' + index); // 标识已经加载过
          } catch (err) {
            commit('DEL_' + index); // 标识加载过程出错，需要下次页面重新加载。
            console.log(err);
          }
        }
      };
    }
  }
  return step;
}

export const state = () => (autoDictionAryState(dictionary));
export const mutations = autoDictionAryMutations(state());
export const actions = autoDictionAryActions(state(), dictionary);
